package memory;

import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        //键盘录入1-100之间的值存入集合，当集合值总和超过200时为止
        ///创建集合
        ArrayList<Integer> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true){//循环
            System.out.println("请输入一个1-100之间的数字：");
            String n = sc.next();
            int num = Integer.parseInt(n);
            if (num < 1 || num > 100){
                System.out.println("输入的数字不在1-100之间");
                continue;
            }
            //自动装箱
            list.add(num);
            int sum = getSum(list);
            if ( sum > 200){
                System.out.println("集合中的数字总和超过200");
                break;
            }
        }
    }
    public static int getSum(ArrayList<Integer> list){
        int sum = 0;
        for (Integer num : list) {
            sum += num;
        }
        return sum;
    }
}
